package cn.wikiflyer.beikang.act;

import java.io.IOException;

import net.tsz.afinal.annotation.view.ViewInject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;

/**
 * 首次打开，用户指导
 */
public class GuideAct extends BaseActivity {

	@ViewInject(id = R.id.viewpager)
	private ViewPager viewPager;

	private int beforePage = 0;
	private final int GUIDE_COUNT = 4;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_guide);
	}

	@Override
	public void init() {

		// 准备数据
		// app().homeUtils.readyData();

		viewPager.setOffscreenPageLimit(1);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			public void onPageSelected(int arg0) {
			}

			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			public void onPageScrollStateChanged(int arg0) {
				if (ViewPager.SCROLL_STATE_IDLE == arg0) {
					if (viewPager.getCurrentItem() == (GUIDE_COUNT - 1)
							&& beforePage == (GUIDE_COUNT - 1)) {
						return;
					} else {
						beforePage = viewPager.getCurrentItem();
					}
				}
			}
		});
		viewPager.setAdapter(new GuideAdapter(this));
		viewPager.setCurrentItem(0, false);

	}

	@Override
	public void setData() {
	}

	class GuideAdapter extends PagerAdapter {
		Context mContext;
		boolean isClicked = false;

		public GuideAdapter(Context context) {
			mContext = context;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			// ImageView imageView = (ImageView) object;
			// app().recycleImageView(imageView);
			// ((ViewPager) container).removeView(imageView);
			((ViewPager) container).removeView((View) object);
		}

		@Override
		public void finishUpdate(View container) {

		}

		@Override
		public int getCount() {
			return GUIDE_COUNT;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			View itemView = LayoutInflater.from(ctx).inflate(
					R.layout.act_guid_item_lay, null);
			switch (position) {
			case 0:
				setBackGround(itemView, "guide1.jpg");
				itemView.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						viewPager.setCurrentItem(1, true);
					}
				});
				break;

			case 1:
				setBackGround(itemView, "guide1.jpg");
				itemView.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						viewPager.setCurrentItem(2, true);
					}
				});
				break;

			case 2:
				setBackGround(itemView, "guide1.jpg");
				itemView.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						viewPager.setCurrentItem(3, true);
					}
				});
				break;
			case 3:
				setBackGround(itemView, "进入APP");
				itemView.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						finish();
						Intent i = new Intent(GuideAct.this, TabAct.class);
						startActivity(i);
					}
				});
				break;
			// case 4:
			// setBackGround(imageView, "welcome6.jpg");
			// imageView.setOnClickListener(new View.OnClickListener() {
			// public void onClick(View v) {
			// finish();
			// Intent i = new Intent(GuideAct.this, TabAct.class);
			// startActivity(i);
			// }
			// });
			// break;
			}
			((ViewPager) container).addView(itemView);
			return itemView;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View container) {

		}
	}

	/**
	 * 将资源文件drawable 用流的形式转为bitmap
	 */
	public void setBackGround(View itemView, String res) {
		((TextView) itemView.findViewById(R.id.textView_guid))
				.setText("" + res);
		;
		// BitmapFactory.Options bmpFactoryOptions = new
		// BitmapFactory.Options();
		// bmpFactoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
		// bmpFactoryOptions.inPurgeable = true;
		// bmpFactoryOptions.inInputShareable = true;
		// bmpFactoryOptions.inSampleSize = getScale(res); // 将图片的大小缩小为1/4
		// Bitmap imageBG;
		// try {
		// imageBG = BitmapFactory.decodeStream(getAssets().open("" + res),
		// null, bmpFactoryOptions);
		// img.setImageBitmap(imageBG);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}

	/**
	 * 获取缩放比例
	 */
	@SuppressLint("NewApi")
	private int getScale(String name) {
		Display currentDisplay = getWindowManager().getDefaultDisplay();
		Point point = new Point();
		currentDisplay.getSize(point);
		int dw = point.x;
		int dh = point.y;
		BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
		bmpOptions.inJustDecodeBounds = true;
		Bitmap bmp = null;
		try {
			bmp = BitmapFactory.decodeStream(getAssets().open("" + name), null,
					bmpOptions);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.e("wj", "bmpWidth : " + bmpOptions.outWidth + " bmpHeight : "
				+ bmpOptions.outHeight);
		int scaleX = bmpOptions.outWidth / dw;
		int scaleY = bmpOptions.outHeight / dh;
		bmpOptions.inJustDecodeBounds = false;
		return (scaleX > scaleY) ? scaleX : scaleY;
	}
}
